﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SQLite;
using Com.Toook.Common;

public partial class nst_ShowPageParamEdit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            LabelId.Text = Request["id"];
            int id;
            if(string.IsNullOrEmpty(LabelId.Text) || !int.TryParse(LabelId.Text, out id))
            {
                LabelId.Text += "，id错误";
                ButtonEdit.Enabled = false;
            }
            else
            {
                InitData();
            }
        }
    }
    private void InitData()
    {   //ExpiryTime, 
        string id = Request["id"];        
        string sql = "select typeId, typeCode, typeName, totalCount, title, keyword, description, newsSiteId, remark " +
            " from ShowPageParam where id=@id";
        SQLiteConnection con = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        cmd.Parameters.Add("@id", DbType.Int32).Value = id;
        SQLiteDataReader reader = null;
       
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            if(reader.Read())
            {
                txtTypeId.Text = reader["typeId"].ToString();
                txtTypeCode.Text = reader["typeCode"].ToString();
                txtTypeName.Text = reader["typeName"].ToString();
                txtTotalCount.Text = reader["totalCount"].ToString();
                txtTitle.Text = reader["title"].ToString();
                txtKeyword.Text = reader["keyword"].ToString();
                txtDescription.Text = reader["description"].ToString();
                txtNewsSiteId.Text = reader["newsSiteId"].ToString();
                txtRemark.Text = reader["remark"].ToString();
                LabelId.Text = id;                
            }
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
        }
        finally
        {
            SqliteHelper.CloseDbResource(con, reader);
        }
    }
    protected void ButtonEdit_Click(object sender, EventArgs e)
    {
        string sql = "update ShowPageParam set typeId=@typeId, typeCode=@typeCode, typeName=@typeName,totalCount=@totalCount," +
            "title=@title, keyword=@keyword, description=@description, newsSiteId=@newsSiteId, remark=@remark " +
            " where id=@id";
        SQLiteConnection con = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        cmd.Parameters.Add("@typeId", DbType.Int32).Value = txtTypeId.Text;
        cmd.Parameters.Add("@typeCode", DbType.AnsiString, 20).Value = txtTypeCode.Text;
        cmd.Parameters.Add("@typeName", DbType.String, 20).Value = txtTypeName.Text;
        cmd.Parameters.Add("@totalCount", DbType.Int32).Value = txtTotalCount.Text;
        cmd.Parameters.Add("@title", DbType.String, 30).Value = txtTitle.Text;
        cmd.Parameters.Add("@keyword", DbType.String, 50).Value = txtKeyword.Text;
        cmd.Parameters.Add("@description", DbType.String, 80).Value = txtDescription.Text;
        cmd.Parameters.Add("@newsSiteId", DbType.AnsiString, 100).Value = txtNewsSiteId.Text;
        cmd.Parameters.Add("@remark", DbType.String, 50).Value = txtRemark.Text;
        cmd.Parameters.Add("@id", DbType.Int32).Value = LabelId.Text;

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            LabelMsg.Text = "修改成功";
            txtTypeId.Text = "";
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
        }
        finally
        {
            SqliteHelper.CloseDbResource(con, null);
        }
    }
}
